package com.gdssoft.pdaweb.jobs;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.CheckWorkOrderDao;
import com.gdssoft.pdaweb.dao.RepairWorkOrderDao;
import com.gdssoft.pdaweb.model.CheckWorkOrder;
import com.gdssoft.pdaweb.model.RepairWorkOrder;
import com.gdssoft.pdaweb.service.EamService;

public class EamSyncRepairOrderStatusJob {
private transient final Log logger = LogFactory.getLog(getClass());
	
	private EamService eamService;
	private RepairWorkOrderDao repairWorkOrderDao;
	
	public EamSyncRepairOrderStatusJob(EamService eamService,RepairWorkOrderDao repairWorkOrderDao) {
		this.eamService = eamService;
		this.repairWorkOrderDao =repairWorkOrderDao;
	} 
	
	public void doJob() {
		logger.info("EamSyncRepairOrderStatusJob execute start");
		
		List<RepairWorkOrder> unSyncRepairWorkOrderList 
				= repairWorkOrderDao.listUnSynchroRepairWorkOrder();
		
		for(int i = 0 ; i <unSyncRepairWorkOrderList.size(); i++ ) {
			RepairWorkOrder repairWorkOrder = unSyncRepairWorkOrderList.get(i);
			Long woId = repairWorkOrder.getAssignmentId();//工单的唯一标识
			boolean bu = eamService.syncRepairOrderStatus(repairWorkOrder);
			if (bu) {
				String NowTime = DateUtil.dateFormat(Calendar.getInstance().getTime(), 
						"yyyy-MM-dd HH:mm:ss");// 系统时间
				
				repairWorkOrder.setFeedbackDt(NowTime);
				
				repairWorkOrderDao.saveOrUpdate(repairWorkOrder);
				
				logger.info("成功同步工单唯一标识为：" + woId + "的维修工单！");
			} else {
				logger.warn("同步工单唯一标识为：" + woId + "的维修工单失败！");
			}
		}
		
		logger.info("EamSyncRepairOrderStatusJob execute end");
	}
}
